package com.valeandre.euroro;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.valeandre.euroro.model.Prices;
import com.valeandre.euroro.model.XmlGold;
import com.valeandre.euroro.net.NetworkService;
import com.valeandre.euroro.net.NetworkServiceListener;

public class LandingActivity extends Activity {

	public final static String EXTRA_MESSAGE = "com.valeandre.euroro.value";
	public final static String GOLD_EXTRA = "com.valeandre.euroro.goldvalue";
	public final static String SILVER_EXTRA = "com.valeandre.euroro.silvervalue";
	public final static String PLATINUM_EXTRA = "com.valeandre.euroro.platinumvalue";
	public final static String PALLADIUM_EXTRA = "com.valeandre.euroro.palladiumvalue";
	public Prices mxmlgold;
	public String mvaluta;
	public double mgoldconversion;
	public double msilverconversion;
	public double mplatinumconversion;
	public double mpalladiumconversion;
	private ProgressDialog mProgressDialog;
	Handler mHandler= new Handler();
	final Context context = this;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_landing);
		
		Button plotButton = (Button) findViewById(R.id.button_plot);

		final Button currencyButton = (Button) findViewById(R.id.edit_message);

		// add button listener
		currencyButton.setOnClickListener(new OnClickListener() {

			public void onClick(View arg0) {

				// custom dialog
				final Dialog dialog = new Dialog(context);
				dialog.setContentView(R.layout.custom);
				dialog.setTitle("Select currency...");

				// set the custom dialog components - text, image and button
				final TextView text = (TextView) dialog.findViewById(R.id.eur);
				text.setText("EUR");
				text.setOnClickListener(new OnClickListener(){

					public void onClick(View arg0) {
						mvaluta="eur";
						mHandler.post(new Runnable() {

							public void run() {
								currencyButton.setText("EUR");
								text.performClick();

							}
						});
						dialog.dismiss();

					}

				});
				final TextView text1 = (TextView) dialog.findViewById(R.id.usd);
				text1.setText("USD");
				text1.setOnClickListener(new OnClickListener(){

					public void onClick(View arg0) {
						mvaluta="usd";
						mHandler.post(new Runnable() {

							public void run() {
								currencyButton.setText("USD");
								text1.performClick();

							}
						});
						dialog.dismiss();


					}

				});
				final TextView text2 = (TextView) dialog.findViewById(R.id.chf);
				text2.setText("CHF");
				text2.setOnClickListener(new OnClickListener(){

					public void onClick(View arg0) {
						mvaluta="chf";
						mHandler.post(new Runnable() {

							public void run() {
								currencyButton.setText("CHF");
								text2.performClick();

							}
						});
						dialog.dismiss();


					}

				});

				dialog.show();
			}

		});
	}


	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

	/** Called when the user clicks the Send button */
	public void GoldConversion(View view) {
		//	EditText editText = (EditText) findViewById(R.id.edit_message);
		showProgress();
		getConversion(mvaluta);
		//    	Intent intent = new Intent(this, DisplayConversionActivity.class);
		//    	
		//    	Double value=0.0; //get the response of the feed
		//    	intent.putExtra(EXTRA_MESSAGE, value);
		//    	startActivity(intent);
	}

	/** Called when the user clicks the Show_plot button */
	public void showPlot(View view) {
		//	EditText editText = (EditText) findViewById(R.id.edit_message);
		//showProgress();

		if(mgoldconversion!=0&&msilverconversion!=0&&mplatinumconversion!=0){
			Intent intent = new Intent(this, DisplayPlotActivity.class);

			intent.putExtra(GOLD_EXTRA, mgoldconversion);
			intent.putExtra(SILVER_EXTRA, msilverconversion);
			intent.putExtra(PLATINUM_EXTRA, mplatinumconversion);
			intent.putExtra(EXTRA_MESSAGE, mvaluta);
			intent.putExtra(PALLADIUM_EXTRA, mpalladiumconversion);
			startActivity(intent);
		}
	}




	public void getConversion(final String valuta) {
		Log.d("OMINO", "getConversion");

		NetworkService.getInstance().getConversionValues(new NetworkServiceListener() {

			@Override
			public void conversionPerformed(Prices xml) {
				Log.d("OMINO", "getSettingsPerformed");
				mxmlgold=xml;
				mHandler.post(new Runnable() {

					public void run() {
						showConversion(valuta);

					}
				});

			}

			@Override
			public void conversionFailed(int statusCode) {
				Log.e("OMINO", "getSettingsFailed " + statusCode);
				mgoldconversion=0;
				msilverconversion=0;
				mplatinumconversion=0;
				mpalladiumconversion=0;
				hideProgress();
				//				showConnectionAlertDialogAsync();
			}

			@Override
			public void conversionFailed(Exception e) {
				Log.e("OMINO", "getSettingsFailed " + e.getMessage());
				
				hideProgress();
				//				showConnectionAlertDialogAsync();
			}
		});

	}


	public void showConversion(String valuta){

		hideProgress();

//		for(int i=0; i<mxmlgold.getPriceList().size();i++){
//			if(mxmlgold.getPriceList().get(i).getCurrency().equalsIgnoreCase(valuta)){
//				for (int j=0; j<mxmlgold.getPriceList().get(i).getPrice().size();j++){
//					if(mxmlgold.getPriceList().get(i).getPrice().get(j).getCommodity().equalsIgnoreCase("gold")){
//						mgoldconversion=mxmlgold.getPriceList().get(i).getPrice().get(j).getValue();
//					}
//					if(mxmlgold.getPriceList().get(i).getPrice().get(j).getCommodity().equalsIgnoreCase("platinum")){
//						mplatinumconversion=mxmlgold.getPriceList().get(i).getPrice().get(j).getValue();
//					}
//					if(mxmlgold.getPriceList().get(i).getPrice().get(j).getCommodity().equalsIgnoreCase("silver")){
//						msilverconversion=mxmlgold.getPriceList().get(i).getPrice().get(j).getValue();
//					}
//					if(mxmlgold.getPriceList().get(i).getPrice().get(j).getCommodity().equalsIgnoreCase("palladium")){
//						mpalladiumconversion=mxmlgold.getPriceList().get(i).getPrice().get(j).getValue();
//					}
//				}
//			}
//		}

		EditText conversion = (EditText) findViewById(R.id.conversion_value);
		CharSequence c= String.valueOf(mgoldconversion);
		conversion.setText(c);

		EditText conversionsilver = (EditText) findViewById(R.id.conversionsilver_value);
		CharSequence csilver= String.valueOf(msilverconversion);
		conversionsilver.setText(csilver);

		EditText conversionplatinum = (EditText) findViewById(R.id.conversionplatinum_value);
		CharSequence cplatinum= String.valueOf(mplatinumconversion);
		conversionplatinum.setText(cplatinum);
		
		EditText conversionpalladium = (EditText) findViewById(R.id.conversionpalladium_value);
		CharSequence cpalladium= String.valueOf(mpalladiumconversion);
		conversionpalladium.setText(cpalladium);
	}


	public void showProgress() {
		mHandler.post(new Runnable() {

			public void run() {
				if (mProgressDialog == null) {
					mProgressDialog = ProgressDialog.show(LandingActivity.this, "Attendere", "Attempting to http://www.xmlcharts.com/precious-metals.html...", true, false);
				}

			}
		});


	}

	public void hideProgress() {
		mHandler.post(new Runnable() {

			public void run() {
				if (mProgressDialog != null && mProgressDialog.isShowing()) {
					mProgressDialog.dismiss();
					mProgressDialog = null;
				}

			}
		});


	}

}
